/**
 * Copyright 2011-2011 - Members of the Chaupal Community
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package net.chaupal.API.Streamable;

import java.nio.ByteBuffer;
import java.util.Map.Entry;

/**
 * Marker interface for objects which can be persisted into a byte array or
 * a string.
 * 
 * @param <T> Type/Class of the implementing object.
 */
public interface Streamable<T> {

    /**
     * Provides a byte array representation of the implementing object.
     *
     * @return a byte array
     */
    public byte[] toByteArray();
    
    /**
     * Writes the byte array representation of the implementing object 
     * into a byte buffer.
     * 
     * @param bb a byte buffer
     * @param pos the starting position for writing
     */
    public void writeIntoByteBuffer(ByteBuffer bb, int pos);
    
    /**
     * Reads a byte array representation of the implementing object type
     * and returns the corresponding instance.
     * 
     * @param bb a byte buffer
     * @param pos the starting position for writing
     * @return an instance of this object type 
     */
    public T readFromByteBuffer(ByteBuffer bb, int pos);

    /**
     * Provides a string representation of this object (value) together with 
     * the corresponding technical name (key).
     * 
     * @return the technical name and object string representation
     */
    public Entry<String,String> toStringEntry();
    
    /**
     * Provides a string representation of this object (value) together with 
     * the corresponding technical name (key).
     * 
     * @param entry the technical name and object string representation
     * @return an instance of this object type 
     */
    public T fromStringEntry(Entry<String,String> entry);

}